package com.quectel.cms.controller.partybuildingtask;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.core.partybuilding.PartyBuildingPlanConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.partybuildingtask.dto.PartyBuildingPlanTaskDto;
import com.quectel.core.module.partybuildingtask.service.PartyBuildingPlanTaskService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:32
 */
@RestController
@RequestMapping("partyBuildingPlanTask")
@Tag(name = "PartyBuildingPlanTaskController", description = "党建任务相关api")
public class PartyBuildingPlanTaskController extends BaseController {

    @DubboReference
    private PartyBuildingPlanTaskService partyBuildingPlanTaskService;

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "partyBuildingPlanTask:list")
    @Operation(summary = "分页获取党建任务列表")
    public Response<Page<PartyBuildingPlanTaskDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());

        Byte completeFlag = request.getByte(PartyBuildingPlanConstants.TASK_COMPLETE_FLAG);
        if(SystemConstants.YES.equals(completeFlag)){
            request.put(LambdaUtil.getFieldName(PartyBuildingPlanTaskDto::getExecStatus),PartyBuildingPlanConstants.TaskStatus.COMPLETED.getStatus());
        }
        if(SystemConstants.NO.equals(completeFlag)){
            request.put(LambdaUtil.getFieldName(PartyBuildingPlanTaskDto::getExecStatus),PartyBuildingPlanConstants.TaskStatus.UNCOMPLETED.getStatus());
        }

        int total = partyBuildingPlanTaskService.queryTotal(request.getParams());

        List<PartyBuildingPlanTaskDto> list = null;
        if (total > 0) {
            list = partyBuildingPlanTaskService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PartyBuildingPlanTaskDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PartyBuildingPlanTaskDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "partyBuildingPlanTask:edit")
    @Operation(summary = "在编辑时根据id查询党建任务详情")
    public Response<PartyBuildingPlanTaskDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyBuildingPlanTaskService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyBuildingPlanTaskDto partyBuildingPlanTaskDto = partyBuildingPlanTaskService.selectById(id);

        return Response.<PartyBuildingPlanTaskDto>ok().wrap(partyBuildingPlanTaskDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "partyBuildingPlanTask:info")
    @Operation(summary = "根据id查询党建任务的详情")
    public Response<PartyBuildingPlanTaskDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyBuildingPlanTaskService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyBuildingPlanTaskDto partyBuildingPlanTaskDto = partyBuildingPlanTaskService.selectById(id);

        return Response.<PartyBuildingPlanTaskDto>ok().wrap(partyBuildingPlanTaskDto);
    }

    /**
     * 保存
     *
     * @param partyBuildingPlanTaskDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "partyBuildingPlanTask:edit")
    @Operation(summary = "新增党建任务")
    public Response<Object> save(@RequestBody @Validated PartyBuildingPlanTaskDto partyBuildingPlanTaskDto) {

        //填充基础数据
        fillSaveCommonData(partyBuildingPlanTaskDto);

        partyBuildingPlanTaskService.save(partyBuildingPlanTaskDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param partyBuildingPlanTaskDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "partyBuildingPlanTask:edit")
    @Operation(summary = "根据id修改党建任务")
    public Response<Object> update(@RequestBody  @Validated PartyBuildingPlanTaskDto partyBuildingPlanTaskDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(partyBuildingPlanTaskDto.getId()));
        if (partyBuildingPlanTaskService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(partyBuildingPlanTaskDto);

        partyBuildingPlanTaskService.updateById(partyBuildingPlanTaskDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "partyBuildingPlanTask:delete")
    @Operation(summary = "根据多条记录id批量删除党建任务")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (partyBuildingPlanTaskService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        partyBuildingPlanTaskService.deleteBatch(ids);

        return Response.ok();
    }
}
